package com.dev.warehouse.bus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dev.warehouse.bus.entity.Goods;
import com.dev.warehouse.bus.entity.Sales;
import com.dev.warehouse.bus.mapper.GoodsMapper;
import com.dev.warehouse.bus.mapper.SalesMapper;
import com.dev.warehouse.bus.service.ISalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;

/**
 * 物品出库Controller类
 */
@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements ISalesService {

    @Autowired
    private GoodsMapper goodsMapper;


    /**
     * 添加物品出库
     * @param entity    物品出库实体类
     * @return
     */
    @Override
    public synchronized boolean save(Sales entity) {
        Goods goods = goodsMapper.selectById(entity.getGoodsid());
        int surplus = goods.getNumber() - entity.getNumber();
        goods.setNumber(surplus);
        //更新物品的库存信息
        goodsMapper.updateById(goods);
        return super.save(entity);
    }

    /**
     * 更新物品出库
     * @param entity    物品出库实体类
     * @return
     */
    @Override
    public boolean updateById(Sales entity) {
        //根据出库单ID查询出库单信息
        Sales sales = baseMapper.selectById(entity.getId());
        Goods goods = goodsMapper.selectById(entity.getGoodsid());
        //仓库物品数量=原库存-出库单修改之前的数量+修改之后的数量
        //     80  +40 -  50     30
        goods.setNumber(goods.getNumber()+sales.getNumber()-entity.getNumber());
        //更新物品
        goodsMapper.updateById(goods);
        return super.updateById(entity);
    }

    /**
     * 删除物品出库信息
     * @param id    物品出库单ID
     * @return
     */
    @Override
    public boolean removeById(Serializable id) {
        //根据物品出库单ID查询出出库单数据
        Sales sales = baseMapper.selectById(id);
        Goods goods = goodsMapper.selectById(sales.getGoodsid());
        //仓库物品数量=原库存+删除物品出库单的数量
        goods.setNumber(goods.getNumber()+sales.getNumber());
        goodsMapper.updateById(goods);
        return super.removeById(id);
    }
}
